package mcsm.GUI.RightPanel;

import javax.swing.JComboBox;
import javax.swing.JPanel;

public class MyDatePickerModel extends JPanel {
	protected JComboBox yearT, monthT, dayT;
	private int width,height, gap;

	public MyDatePickerModel(int x, int y, int width, int height) {
		this.setLayout(null);
		this.gap = getGap();
		this.setBounds(x, y, width + 3*gap, height);
		this.width = width;
		this.height = height;
		
		setDefaultYears();
		setMonths();
		setDays();
		this.setOpaque(false);
		this.setVisible(true);
	}
	
	protected int getGap() {
		return 3;
	}
	
	protected String[] format(String beginStr, String endStr) {
		int begin, end;
		try {
			begin = Integer.parseInt(beginStr);
			end =  Integer.parseInt(endStr);
		} catch (NumberFormatException ex) {
			return null;
		}
		
		if (begin > end) {
			return null;
		}
		
		int len = end-begin+1;
		String[] item = new String[len];
		for (int i = 0; i < len; i++) {
			item[i] = (begin+i) + "";
		}
		
		return item;
	}
	
	public void setYears(String begin, String end) {
		String[] years = format(begin,end);
		if (years != null) {
			yearT.removeAllItems();
			for (int i = 0; i < years.length; i++) {
				yearT.addItem(years[i]);
			}
		}
	}
	
	protected void setDefaultYears() {
		yearT = new JComboBox();
		yearT.setBounds(0, 0, width*3/7, height);
		this.add(yearT);
		setYears("2011","2015");
	}
	
	protected void setMonths() {
		String[] months = format("1","12");
		monthT = new JComboBox(months);
		int x = yearT.getX() + yearT.getWidth() + gap;
		monthT.setBounds(x, 0, width*2/7, height);
		this.add(monthT);
	}
	
	protected void setDays() {
		String[] days = format("1","31");
		dayT = new JComboBox(days);
		int x = monthT.getX() + monthT.getWidth() + gap;
		dayT.setBounds(x, 0, width*2/7, height);
		this.add(dayT);
	}
	
	public String getDate() {
		String y = (String)yearT.getSelectedItem();
		String m = (String)monthT.getSelectedItem();
		String d = (String)dayT.getSelectedItem();
		
		return y+"-"+m+"-"+d;
	}
	
	public String getText() {
		return getDate();
	}
}
